#Nature's Kidneys: the Role of Wetland Reserve Easements in Restoring Water Quality
#Nicole Karwowski and Marin Skidmore
#Replication File
#2025

#Watershed MARB boundaries
#create shapefile with hucs in our main sample

#load libraries
library(ggplot2)
library(raster)
library(sf)
library(exactextractr)
library(Rcpp)
library(data.table)
library(tidyverse)
library(Hmisc)
library(haven)
library(stargazer)
library(purrr)
library(tidyr)
library(collapse)
library(ggthemes)
library(scales)
library(tigris)
options(tigris_use_cache = TRUE)
library(mapview)
options(scipen=999)
library(stringr)
library(data.table)
library(lubridate)
library(units)
library(geosphere)
library(plm)
library(lmtest)
library(sandwich)
library(fixest)
library(did)
library(stargazer)
library(scales)
library(terra)
library(CropScapeR) 
library(reshape2)
library(mapview)
library(leaflet)


#wd
#identify the hucs and extent we need
#only need MARB area with water quality readings. 

#open up the snapd data to get relevant hucs
setwd("C:/Users/16308/OneDrive - Montana State University/UW_Madison/Water quality/Replication Package")
load("Data/SNAPD/Processed/SNAPwq_huc_ym.RData")

#open the huc12 data
huc12<-st_read("Data/WatershedBoundaries/Raw/USGS_WBD/USGS_WBD.shp")
huc12<-subset(huc12, select=c(huc12, tohuc, areaacres, shape_area, geometry))

MARBhuc<-unique(wq_huc12$huc12)
MARB<-subset(huc12, huc12 %in% MARBhuc)

MARB<- mutate(MARB, ID=row_number())
MARB<-MARB %>% relocate(ID, .before=huc12)

length(unique(MARB$huc12))
length(unique(MARB$ID))

extent(huc12)
extent(MARB)

mapview::viewExtent(MARB)
mapview(MARB)

#save the MARB as shapefile
st_write(MARB, "Data/WatershedBoundaries/Processed/MARB/MARB.shp", driver="ESRI Shapefile")
